//
// Created by 吴喆 on 2020/3/1.
//

#include "ConfigUtil.h"

const std::string ConfigConst::SCREEN_WIDTH = "SCREEN_WIDTH";/*NOLINT*/
const std::string ConfigConst::SCREEN_HEIGHT = "SCREEN_HEIGHT";/*NOLINT*/
const std::string ConfigConst::VERTICAL_SYN = "VERTICAL_SYN";/*NOLINT*/
const std::string ConfigConst::GAME_TITLE = "GAME_TITLE";/*NOLINT*/
const std::string ConfigConst::MATERIAL_PATH = "MATERIAL_PATH";/*NOLINT*/
const std::string ConfigConst::MAP_PATH = "MAP_PATH";/*NOLINT*/

bool ConfigUtil::initialized = false;/*NOLINT*/
std::unordered_map<std::string, std::string> ConfigUtil::configMap{100};/*NOLINT*/

bool ConfigUtil::loadConfig() {
    std::ifstream ifStream("../config.propertis", std::ios_base::in | std::ios_base::out);
    if (!ifStream) {
        std::cout << "open file failed" << std::endl;
        return false;
    }
    std::string s;
    int pos;
    std::string key;
    std::string val;
    while (std::getline(ifStream, s)) {
        pos = s.find('=');
        key = s.substr(0, pos);
        val = s.substr(pos + 1, s.length() - 1);
        configMap[key] = val;
    }
    initialized = true;
    return true;
}

std::string ConfigUtil::getProp(const std::string &key) {
    return configMap[key];
}

double ConfigUtil::getPropD(const std::string &key) {
    return std::stod(configMap[key]);
}

int ConfigUtil::getPropI(const std::string &key) {
    return std::stoi(configMap[key]);
}

bool ConfigUtil::init() {
    return ConfigUtil::loadConfig();

}
